/**
 * 
 */
package models;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author Administrator
 *
 */
public class AdvancedBot extends Player {
	
	public final byte MO_FOLD = 0;
	public final byte MO_BET = 1;
	public final byte MO_CHECK = 2;
	public final byte MO_RAISE = 3;
	public final byte MO_CALL = 4;
	public final byte MO_ALLIN = 5;
	public final byte MO_REPLACE = 6;
	public final byte MO_READY = 7;

	public final byte AB_NONE = 0;
	public final byte AB_CHECKBETFOLD = 1;
	public final byte AB_RAISECALLFOLD = 2;
	public final byte AB_FOLDALLIN = 3;
	public final byte AB_REPLACE = 4;
	public final byte AB_CONFIRM = 5;
	
	Random generator;
	

	/**
	 * @param t
	 * @param name
	 */
	public AdvancedBot(final Table table, final String name, final int to, final int i) {
		super(table, name, to, i);
		generator = new Random();
		// TODO Auto-generated constructor stub
	}
	
	public boolean isHuman() {
		return false;
	}
	

	public boolean isBot() {
		return true;
	}
	
	@Override
	public void yourTurn(byte ability, int min) 
	{
		executeAdvancedAI(ability,min);
	}
	
	
	private void executeAdvancedAI(byte ability, int min) {
		
		switch(ability)
		{
			case AB_CHECKBETFOLD:
				if(tokens == 1)
				{
					move(MO_CHECK,0);
				}
				if(Evaluate.getHandScore(this).hand == 0)
				{
					if(generator.nextBoolean())
						move(MO_CHECK,0);
					else
						move(MO_FOLD,0);
					break;
				}				
				move(MO_BET,(tokens/10)+1);
				break;
			
			case AB_RAISECALLFOLD:
				if(Evaluate.getHandScore(this).hand == 0)
				{
					if(generator.nextInt(15)<12)
						move(MO_FOLD,0);
					else
						move(MO_CALL,0);
					break;
				}
				if(Evaluate.getHandScore(this).hand <= 2)
				{
					move(MO_CALL,0);
					break;
				}
				try {
					int b = generator.nextInt((tokens-min)/2 +1) + min;
					if(b == min)
						b++;
					if(b < min)
						b+=2;
					move(MO_RAISE,b);
				} catch (IllegalArgumentException e)
				{
					System.out.println(Integer.toString(min));
					System.out.println(Integer.toString(tokens));
					System.exit(0);
				}
				break;
				
			case AB_FOLDALLIN:
				if(Evaluate.getHandScore(this).hand > 2)
					move(MO_ALLIN,0);
				else
					if(tokens<100)
						move(MO_ALLIN,0);
					else move(MO_FOLD,0);
				break;
			
			case AB_REPLACE:
				List<Card> exc = new ArrayList<Card>(Evaluate.getExchangeProposition(this));
				try { move(exc); } catch (PokerException e) {}
				break;
			
			case AB_CONFIRM:
				//sleep(1000);
				move();
				break;
				
		}
		
		
	}
}
